package com.bole.controller;


import com.bole.dao.CarSpecDao;
import com.bole.dto.user.DicCarSpecDto;
import com.bole.pojo.DicCarSpecDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("spec")
@Api(value="规格接口")
public class CarSpecController {
    @Autowired
    private CarSpecDao specDao;

    @GetMapping("getSpecInfo")
    @ApiOperation(value = "查询规格")
    public List<DicCarSpecDto> getSpecInfo() {
        List<DicCarSpecDo> all = specDao.findAll();
        ArrayList<DicCarSpecDto> dicContentTypeDtoArrayList = new ArrayList<>();
        for (DicCarSpecDo dicCarSpecDo : all) {
            DicCarSpecDto dicCarSpecDto = new DicCarSpecDto();
            BeanUtils.copyProperties(dicCarSpecDo, dicCarSpecDto);
            dicContentTypeDtoArrayList.add(dicCarSpecDto);
        }
        return dicContentTypeDtoArrayList;

    }


    @PostMapping("saveSpecInfo")
    @ApiOperation(value = "保存规格")
    @ApiImplicitParam(name = "dicCarSpecDo",value = "规格",required = true,dataTypeClass = DicCarSpecDo.class)
    public void saveContentCateInfo(@RequestBody DicCarSpecDo carSpecDo) {
        DicCarSpecDo  dicCarSpecDo = specDao.save(carSpecDo);
        DicCarSpecDto dicCarSpecDto = new DicCarSpecDto();
        BeanUtils.copyProperties(dicCarSpecDo, dicCarSpecDto);
    }


    @PostMapping("toUpdate")
    @ApiOperation(value = "回显")
    @ApiImplicitParam(name = "id",value = "规格id",required = true,dataTypeClass = Long.class)
    public DicCarSpecDto toUpdate(@RequestParam("id") Long id) {
        DicCarSpecDo dicCarSpecDo = specDao.findById(id).get();
        DicCarSpecDto dicCarSpecDto = new DicCarSpecDto();
        BeanUtils.copyProperties(dicCarSpecDo, dicCarSpecDto);
        return dicCarSpecDto;
    }

    @PostMapping("updateSpecInfo")
    @ApiOperation(value = "修改规格")
    @ApiImplicitParam(name = "dicCarSpecDo",value = "规格",required = true,dataTypeClass = DicCarSpecDo.class)
    public void updateSpecInfo(@RequestBody DicCarSpecDo dicCarSpecDo) {
        DicCarSpecDo carSpecDo = specDao.save(dicCarSpecDo);
        DicCarSpecDto dicCarSpecDto = new DicCarSpecDto();
        BeanUtils.copyProperties(carSpecDo, dicCarSpecDto);
    }


    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ides",value = "规格",required = true,dataTypeClass = Long.class)
    public void deleteBatch(@RequestParam("ides") List<Long> ides) {
        specDao.deleteBatch(ides);
    }




}
